package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.system.pojo.SysMenu;
import com.aisile.crowd_funding.system.pojo.SysMenuExample;
import com.aisile.crowd_funding.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("sys/menu")
public class MenuColltroller {

    public static final String VIEW_PATH = "system/menu/";

    @Autowired
    MenuService menuService;


    /**
     * 跳转菜单页面
     */
    @RequestMapping
    public String Menu() {

        return VIEW_PATH + "menu";
    }

    /**
     * 查询菜单列表
     */
    @RequestMapping("list")
    @ResponseBody
    public List<SysMenu> list() {

        return menuService.selectByExample(null);
    }

    /**
     * 跳转添加页面
     *
     * @return
     */
    @RequestMapping("add")
    public String add(@RequestParam(value = "parentId", defaultValue = "0") Long parentId, Model model) {

        if (parentId == 0) {

            model.addAttribute("pName", "根目录");

        } else {

            SysMenu sysMenu = menuService.selectByPrimaryKey(parentId);
            model.addAttribute("pName", sysMenu.getName());

        }
        model.addAttribute("parentId", parentId);
        return VIEW_PATH + "add";


    }

    /**
     * 添加的方法
     */
    @RequestMapping("save")
    @ResponseBody
    public R save(SysMenu sysMenu) {

        int insert = menuService.insert(sysMenu);
        if (insert>0) {

            return R.ok();
        } else {

            return R.error();
        }


    }
    @RequestMapping("edit")
    public String edit(Long menuId,Model model){
        SysMenu sysMenu = menuService.selectByPrimaryKey(menuId);
        //父节点
        if(sysMenu.getParentId()==0){
            model.addAttribute("pName","根目录");
        }else {
            //查询他的父Id
            SysMenu parent = menuService.selectByPrimaryKey(sysMenu.getParentId());
            model.addAttribute("pName",sysMenu.getName());
        }
        model.addAttribute("menu",sysMenu);
        return VIEW_PATH+"edit";
    }
    /**
     * 修改菜单列表
     * */
    @RequestMapping("update")
    @ResponseBody
    public R update(SysMenu sysMenu){

        int update = menuService.updateByPrimaryKey(sysMenu);

        if(update>0){

            return R.ok();
        }else{

            return R.error();
        }

    }

    /**
     * 删除菜单
     *
     * */
    @RequestMapping("del")
    @ResponseBody
    public R del(Long menuId){

        int i =menuService.selectChildCount(menuId);

        if(i>0){
            //有字节点
            return  R.error("请先删除字节点");
        }else {
            int i1 = menuService.deleteByPrimaryKey(menuId);
            if(i1>0){
                return R.ok();
            }

        }
        return R.error();
    }
}